#!/usr/bin/perl -w

# TODO: Handle undefined $1
# TODO: Lock files

use LWP::UserAgent;
use URI::Escape;

use FCTranscode;


$ua = LWP::UserAgent->new;
$ua->agent("pool.pl/0.1");

$http_req = HTTP::Request->new(GET => "http://$remote_server.$req_script");
$http_req->protocol("HTTP/1.1");

$resp = $ua->request($http_req); #TODO - handle error

if ($resp->is_success){
	$reply = $resp->content;
	print "Reply-----\n$reply\n-----\n";
	open(FH, "<", \$reply); #Handle error
	@a_reply = <FH>;
	close FH;

	# At the moment poll.pl is only handling TRANSCODE
	if ($a_reply[0] ne "TRANSCODE") {print "Invalid Action\n"; exit; }
	
	#print "Array size: $#a_reply\n";
	for ($line_n=1 ;$line_n<$#a_reply ;$line_n+=2 ){

		$file=$a_reply[$line_n];
		chomp($file);
		$formats = $a_reply[$line_n+1];
		chomp($formats);

		$cmd = "$conv_loc\"$file\" \"$formats\"";
		print $cmd."\n";
		$output = `$cmd`;	# Check for error? Perhaps not as conv will generate them
		print $output;

		#No need to split formats since conv.pl already does that 

		#print "File: $file\n";
		#@formats=split(/\&/, $a_reply[$line_n+1]);
		#foreach $format (@formats){
		#	chomp($format);
		#	$cmd = "$conv_loc \"$file\" \"$format\"";
		#	print $cmd."\n";
		#	#$output = `$cmd`;
		#	#print $output;
		#}
	}
}
else {
	# Change bellow to a proper error message
	print 'Status_line: '.$resp->status_line."\n".$resp->status_line."\n";
}

